package cn.edu.zafu.bigdata.smj.f;

import java.io.*;
import java.sql.*;
import java.util.Scanner;

class Student implements Serializable {
    String id;
    String name;
    String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student: id=" + id + ", name=" + name + ", phone=" + phone;
    }

    // Getters and setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

class DatabaseManager {
    private static final String URL = "jdbc:mysql://192.168.169.76:3306/learnjava";
    private static final String USER = "learn";
    private static final String PASSWORD = "learnpassword";

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }

    public static void createTable() {
        String sql = "CREATE TABLE IF NOT EXISTS students_202203090110 (" +
                "id VARCHAR(255) PRIMARY KEY, " +
                "name VARCHAR(255), " +
                "phone VARCHAR(255))";
        try (Connection conn = getConnection();
             Statement stmt = conn.createStatement()) {
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void addStudent(Student student) {
        String sql = "INSERT INTO students_202203090110 (id, name, phone) VALUES (?, ?, ?)";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, student.getId());
            pstmt.setString(2, student.getName());
            pstmt.setString(3, student.getPhone());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Student findStudent(String id) {
        String sql = "SELECT * FROM students_202203090110 WHERE id = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                return new Student(rs.getString("id"), rs.getString("name"), rs.getString("phone"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void updateStudent(Student student) {
        String sql = "UPDATE students_202203090110 SET name = ?, phone = ? WHERE id = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, student.getName());
            pstmt.setString(2, student.getPhone());
            pstmt.setString(3, student.getId());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void deleteStudent(String id) {
        String sql = "DELETE FROM students_202203090110 WHERE id = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

class MStudent {
    public static void addStudent(Scanner scanner) {
        System.out.print("id: ");
        String id = scanner.nextLine();
        if (DatabaseManager.findStudent(id) != null) {
            System.out.println("id重复，请重新输入。");
            return;
        }
        System.out.print("name: ");
        String name = scanner.nextLine();
        System.out.print("phone: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        DatabaseManager.addStudent(student);
        System.out.println("OK!");
    }

    public static void findStudent(Scanner scanner) {
        System.out.print("id: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            Student student = DatabaseManager.findStudent(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("id（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }

    public static void updateStudent(Scanner scanner) {
        System.out.print("id: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            Student student = DatabaseManager.findStudent(id);
            if (student != null) {
                System.out.print("new name: ");
                String name = scanner.nextLine();
                System.out.print("new phone: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                DatabaseManager.updateStudent(student);
                System.out.println("already update!");
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("id（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }

    public static void deleteStudent(Scanner scanner) {
        System.out.print("id: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            if (DatabaseManager.findStudent(id) != null) {
                DatabaseManager.deleteStudent(id);
                System.out.println("学生信息已删除。");
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("id（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }
}

public class TestStudent {
    public static void main(String[] args) {
        DatabaseManager.createTable();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int p = scanner.nextInt();
            scanner.nextLine();

            switch (p) {
                case 1:
                    MStudent.addStudent(scanner);
                    break;
                case 2:
                    MStudent.findStudent(scanner);
                    break;
                case 3:
                    MStudent.updateStudent(scanner);
                    break;
                case 4:
                    MStudent.deleteStudent(scanner);
                    break;
                case 5:
                    System.out.println("程序已退出。");
                    System.exit(0);
                    break;
            }
        }
    }
}